Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(shaderMaterial): infer uniforms types #2375

Merged
merged 2 commits into from
Feb 26, 2025

Conversation

CodyJasonBennett
Copy link
Member

Infers the returned parameters and uniforms, whereas this was not typesafe before.

import { shaderMaterial } from '@react-three/drei'

const CustomMaterial = shaderMaterial({ time: 0 }, vertexShader, fragmentShader)
const material = new CustomMaterial()
material.onBeforeRender = () => material.time++

Additionally, the returned class can be used for type-safe extending as JSX.

import { extend, ThreeElement } from '@react-three/fiber'
import { shaderMaterial } from '@react-three/drei'

const CustomMaterial = shaderMaterial({ time: 0 }, vertexShader, fragmentShader)

declare module '@react-three/fiber' {
  interface ThreeElements {
    customMaterial: ThreeElement<typeof CustomMaterial>
  }
}

extend({ CustomMaterial })
// alternatively,
// const Component = extend(CustomMaterial)

<customMaterial time={0} />

Copy link

vercel bot commented Feb 26, 2025

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated (UTC)
drei ✅ Ready (Inspect) Visit Preview 💬 Add feedback Feb 26, 2025 6:20pm

Copy link

This pull request is automatically built and testable in CodeSandbox.

To see build info of the built libraries, click here or the icon next to each commit SHA.

@CodyJasonBennett CodyJasonBennett merged commit 53c0dec into master Feb 26, 2025
5 checks passed
@CodyJasonBennett CodyJasonBennett deleted the fix/shadermaterial-infer-uniforms branch February 26, 2025 18:25
Copy link

🎉 This PR is included in version 10.0.3 🎉

The release is available on:

Your semantic-release bot 📦🚀

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant